package org.kineticsproject.application;


import java.awt.Color;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.JFreeChart;
import org.jfree.experimental.chart.swt.ChartComposite;
import org.kineticsproject.devices.parsers.UnitConverter;
import org.kineticsproject.visualizations.HeartRatePieChart;
import org.kineticsproject.visualizations.JPanelExt;
import org.kineticsproject.visualizations.MultiplePlotChart;
import org.kineticsproject.visualizations.SinglePlotChartMultipleAxis;
import org.kineticsproject.visualizations.XYSeriesExt;

import data.DataSample;
import data.Exercise;
import data.FusedDataSample;
import data.State;

import java.util.LinkedList;


public class XYTimeSeriesView extends ViewPart {

	public static final String ID = "org.kineticsproject.application.xytimeseriesview";

	State	state;
	
	FusedDataSample fsample;
	double speedSample = 0;
	float totalDisplacement = 0;
	
	@Override
	public void createPartControl(Composite parent) {
		
		MultiplePlotChart multipleChart = new MultiplePlotChart();
		
		List<XYSeriesExt> xyTimeSeries;
		
		//List<JPanelExt> chartpanelList = new LinkedList<JPanelExt>();
		
		SinglePlotChartMultipleAxis singleChart = new SinglePlotChartMultipleAxis();
		
		XYSeriesExt hrTimeSeries = new XYSeriesExt("Heart Rate", "Seconds",
				"Beats/Minute", "Time", "Heart Rate", Color.red, Color.red);
		
		XYSeriesExt altTimeSeries = new XYSeriesExt("Altitude", "Seconds",
				"Feet", "Time", "Altitude", Color.blue, Color.blue);
		
		XYSeriesExt speedTimeSeries = new XYSeriesExt("Speed", "Seconds",
				"Miles/Hour", "Time", "Speed", Color.black, Color.black);
		
		XYSeriesExt distanceTimeSeries = new XYSeriesExt("Distance", "Seconds",
				"Miles", "Time", "Distance", Color.green, Color.green);
		
		state = State.getInstance();
		
		Exercise exercise = state.getActiveExercise();
		
		xyTimeSeries = new LinkedList<XYSeriesExt>();
		
		
		Iterator<DataSample> it = exercise.getFusedDataLog().getDataSamples().iterator();
		
		int i = 0;
		
		while(it.hasNext())
		{
			fsample = (FusedDataSample)it.next();
			
			totalDisplacement = totalDisplacement
			+ fsample.getDisplacementsample().getDisplacement();
			
			speedSample = UnitConverter.lowpassfilter(0.05, speedSample,
					fsample.getDisplacementsample().getSpeed());
			
			
			
			
			// cheap downsampling
		//	if (i == 70)
			{
				speedTimeSeries.add(fsample.getTimestamp().getTime(),
						UnitConverter.KilometerstoMiles(speedSample));
				
				distanceTimeSeries.add(fsample.getTimestamp().getTime(),
						UnitConverter.FeettoMiles(totalDisplacement));
				
				altTimeSeries.add(fsample.getTimestamp().getTime(), fsample
						.getPositionsample().getAltitude());
				
				hrTimeSeries.add(fsample.getTimestamp().getTime(), fsample.getHeartratesample().getHeartRate());
				
				i = 0;
			}
			i++;
			
			
			/*distanceTimeSeries.calculateStatistics();
			speedTimeSeries.calculateStatistics();
			altTimeSeries.calculateStatistics();
			hrTimeSeries.calculateStatistics();
			*/
			
		}
		


		xyTimeSeries.add(hrTimeSeries);
		xyTimeSeries.add(speedTimeSeries);
		xyTimeSeries.add(altTimeSeries);
		xyTimeSeries.add(distanceTimeSeries);
		
		singleChart.setDisplayPlotSelection(true);
		
		//JPanelExt ext = singleChart.setupChart(xyTimeSeries,"XY Time Series of Fused Data","XY Time Series of Fused Data");

		JFreeChart chart2 = multipleChart.setupChart(xyTimeSeries, "Time Series", "Time Series", new
		 org.jfree.chart.renderer.xy.StandardXYItemRenderer());
		 
		//HeartRatePieChart hr = new org.kineticsproject.visualizations.HeartRatePieChart();
		//JFreeChart chart = hr.setupChart(exercise, "my heart rate");
		ChartComposite frame = new ChartComposite(parent, SWT.FILL,chart2);
		//ChartComposite a = new ChartComposite()
		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

}
